﻿using Microsoft.Xna.Framework;

namespace SceneManager
{
	public class SceneManagerComponent : DrawableGameComponent, ISceneManager
	{
		#region Constructors

		public SceneManagerComponent(Game game)
			: base(game)
		{
			game.Services.AddService<ISceneManager>(this);
		}

		#endregion

		#region Properties

		public Scene Scene { get; private set; }

		#endregion

		#region Methods

		protected override void Dispose(bool disposing)
		{
			if (Scene != null)
			{
				Scene.UnloadContent();
			}

			Game.Services.RemoveService<ISceneManager>();
			base.Dispose(disposing);
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);

			if (Scene != null)
			{
				Scene.Update(gameTime);
			}
		}

		public override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);

			if (Scene != null)
			{
				Scene.Draw(gameTime);
			}
		}

		public Scene LoadScene(Scene scene)
		{
			if (Scene != null)
			{
				Scene.UnloadContent();
			}
			Scene = scene;
			if (Scene != null)
			{
				Scene.LoadContent(GraphicsDevice);
			}
			return scene;
		}

		#endregion
	}
}